/**
 * 
 */
package mdb.com.vn.wservices.utils;

import java.math.BigDecimal;
import java.rmi.RemoteException;
import java.util.Date;

import javax.xml.rpc.ServiceException;

import mdb.com.vn.model.FixedDeposits;
import mdb.com.vn.ws.t24gold.T24GoldLocator;
import mdb.com.vn.ws.t24gold.T24GoldSoap;
import mdb.com.vn.wservices.T24Locator;
import mdb.com.vn.wservices.T24Soap;

/**
 * @author tu.dod
 * 
 */
public class FixedDepositsFromT24 {

	public static FixedDeposits getFDInfoFromT24(FixedDeposits fixedDeposits,
			String savingBookNo) throws RemoteException, ServiceException {
		T24Soap t24Soap = new T24Locator().getT24Soap();
		//T24GoldSoap t24Soap = new T24GoldLocator().getT24GoldSoap();
		String[][][] fdResult = null;
		String[][] fdResultCus = null;
		String strmsg = String
				.format("LIST FBNK.AZ.ACCOUNT WITH @ID EQ %s PRINCIPAL INTEREST.RATE LOCAL.REF CUSTOMER CO.CODE VALUE.DATE MATURITY.DATE INPUTTER ALL.IN.ONE.PRODUCT",
						savingBookNo);
		fdResult = t24Soap.JQL2(strmsg);
		if (fdResult.length != 0) {
			fixedDeposits.setSavingbookNo(savingBookNo);
			fixedDeposits.setAmount(new BigDecimal(fdResult[0][1][0]));
			fixedDeposits
					.setInterestRate(Double.parseDouble(fdResult[0][2][0]));
			fixedDeposits.setTerm(fdResult[0][3][3]);
			fixedDeposits.setCustomerId(fdResult[0][4][0]);
			fixedDeposits.setBranchCode(fdResult[0][5][0]);
			fixedDeposits.setStartDate(WSUtils.stringToDate(fdResult[0][6][0],
					"yyyyMMdd"));
			fixedDeposits.setEndDate(WSUtils.stringToDate(fdResult[0][7][0],
					"yyyyMMdd"));
			fixedDeposits.setCreatedBy(fdResult[0][8][0]);
			String strmsgcus = String
					.format("LIST FBNK.CUSTOMER WITH @ID EQ %s SHORT.NAME STREET TOWN.COUNTRY SECTOR",
							fixedDeposits.getCustomerId());
			fdResultCus = t24Soap.JQL1(strmsgcus);
			fixedDeposits.setCustomerName(fdResultCus[0][1]);
			fixedDeposits.setAddress(fdResultCus[0][2] + fdResultCus[0][3]);
			fixedDeposits.setProductCode(fdResult[0][9][0]);
			fixedDeposits.setDateCreated(new Date());
			//just test
//			fixedDeposits.setProductCode("PROMO-FD1");
//			fixedDeposits.setAmount(new BigDecimal("1500000000"));
//			fixedDeposits
//			.setInterestRate(Double.parseDouble("6.8"));
//			fixedDeposits.setTerm("3M");
//			fixedDeposits.setStartDate(WSUtils.stringToDate("20131209",
//					"yyyyMMdd"));
			//fixedDeposits.setCustomerId("1000508110");
			//end
			String productCode = fixedDeposits.getProductCode();
			String customerId = fixedDeposits.getCustomerId();
			if (customerId.substring(0, 1).equals("2")) {
				fixedDeposits.setCustomerType(2);
				if(productCode.equals("PROMO-FD1SME")||productCode.equals("PROMO-FD2SME")){		
					fixedDeposits.setLuckyDraw(WSUtils.CalculateLDRFDCorp(
							fixedDeposits.getTerm(),
							fixedDeposits.getInterestRate(),
							fixedDeposits.getAmount()));
					fixedDeposits.setScCard(WSUtils.CalculateLDRFDCorp(
							fixedDeposits.getTerm(),
							fixedDeposits.getInterestRate(),
							fixedDeposits.getAmount()));
					fixedDeposits.setVoucherGiftMore(WSUtils
							.CalculateGiftVoucher(fixedDeposits.getScCard()));
				}
			}
			if (customerId.substring(0, 1).equals("1")) {
				fixedDeposits.setCustomerType(1);
				if(productCode.equals("PROMO-FD1")||productCode.equals("PROMO-FD2")){			
					fixedDeposits.setLuckyDraw(WSUtils.CalculateLDRFD(
							fixedDeposits.getTerm(),
							fixedDeposits.getInterestRate(),
							fixedDeposits.getAmount()));
					fixedDeposits.setScCard(WSUtils.CalculateLDRFD(
							fixedDeposits.getTerm(),
							fixedDeposits.getInterestRate(),
							fixedDeposits.getAmount()));

					fixedDeposits.setBagGift(WSUtils.CalculateGiftBag(fixedDeposits
							.getScCard()));
					fixedDeposits.setTrayGift(WSUtils
							.CalculateGiftTray(fixedDeposits.getScCard()));
				}
			}
		}
		return fixedDeposits;
	}

}
